package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.IotCubeAc;
import com.ruoyi.system.domain.IotCubeLcValve;
import com.ruoyi.system.domain.SystemControlDTO;
import com.ruoyi.system.mapper.IotCubeAcMapper;
import com.ruoyi.system.mapper.IotCubeLcValveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotCubeLcMapper;
import com.ruoyi.system.domain.IotCubeLc;
import com.ruoyi.system.service.IIotCubeLcService;

/**
 * IoT数据_六面控温水冷机组数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class IotCubeLcServiceImpl extends ServiceImpl<IotCubeLcMapper,IotCubeLc> implements IIotCubeLcService
{
    @Autowired
    private IotCubeLcMapper iotCubeLcMapper;
    @Autowired
    private IotCubeLcValveMapper iotCubeLcValveMapper;
    @Autowired
    private IotCubeAcMapper iotCubeAcMapper;

    /**
     * 查询IoT数据_六面控温水冷机组数据
     * 
     * @param id IoT数据_六面控温水冷机组数据主键
     * @return IoT数据_六面控温水冷机组数据
     */
    @Override
    public IotCubeLc selectIotCubeLcById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询IoT数据_六面控温水冷机组数据列表
     * 
     * @param iotCubeLc IoT数据_六面控温水冷机组数据
     * @return IoT数据_六面控温水冷机组数据
     */
    @Override
    public List<IotCubeLc> selectIotCubeLcList(IotCubeLc iotCubeLc)
    {
        return iotCubeLcMapper.selectIotCubeLcList(iotCubeLc);
    }

    /**
     * 新增IoT数据_六面控温水冷机组数据
     * 
     * @param iotCubeLc IoT数据_六面控温水冷机组数据
     * @return 结果
     */
    @Override
    public boolean insertIotCubeLc(IotCubeLc iotCubeLc)
    {
        return this.save(iotCubeLc);
    }

    /**
     * 修改IoT数据_六面控温水冷机组数据
     * 
     * @param iotCubeLc IoT数据_六面控温水冷机组数据
     * @return 结果
     */
    @Override
    public boolean updateIotCubeLc(IotCubeLc iotCubeLc)
    {
        return this.updateById(iotCubeLc);
    }

    /**
     * 批量删除IoT数据_六面控温水冷机组数据
     * 
     * @param ids 需要删除的IoT数据_六面控温水冷机组数据主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubeLcByIds(Long[] ids)
    {
        return super.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除IoT数据_六面控温水冷机组数据信息
     * 
     * @param id IoT数据_六面控温水冷机组数据主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubeLcById(Long id)
    {
        return this.removeById(id);
    }

    @Override
    public SystemControlDTO getSystemControl(Long warehouseId, Integer type) {
        SystemControlDTO systemControlDTO = new SystemControlDTO();
        List<IotCubeLc> iotCubeLcs = iotCubeLcMapper.selectList(new LambdaQueryWrapper<IotCubeLc>().eq(IotCubeLc::getWarehouseId, warehouseId));
        IotCubeLc iotCubeLc = null;
        if(CollectionUtil.isNotEmpty(iotCubeLcs)){
            if(type ==1){
                iotCubeLc = iotCubeLcs.get(0);
            }else {
                iotCubeLc = iotCubeLcs.get(1);
            }
        }
        if(iotCubeLc != null){
            systemControlDTO.setIotCubeLc(iotCubeLc);

            List<IotCubeLcValve> iotCubeLcValves = iotCubeLcValveMapper.selectList(new LambdaQueryWrapper<IotCubeLcValve>().eq(IotCubeLcValve::getLcId, iotCubeLc.getId()));
            List<IotCubeLcValve> collect = iotCubeLcValves.stream().sorted(Comparator.comparing(IotCubeLcValve::getId)).collect(Collectors.toList());
            collect.forEach(iotCubeLcValve -> {
                if(iotCubeLcValve.getValveOpen()!= null){
                    iotCubeLcValve.setValveOpen(iotCubeLcValve.getValveOpen().multiply(new BigDecimal(100)));
                }
            });
            systemControlDTO.setIotCubeLcValves(collect);
        }

        List<IotCubeAc> iotCubeAcs = iotCubeAcMapper.selectList(new LambdaQueryWrapper<IotCubeAc>().eq(IotCubeAc::getWarehouseId, warehouseId));
        if(CollectionUtil.isNotEmpty(iotCubeAcs)){
            IotCubeAc iotCubeAc;
            if(type ==1){
                iotCubeAc = iotCubeAcs.get(0);
            }else {
                if(iotCubeAcs.size() == 1){
                    iotCubeAc = iotCubeAcs.get(0);
                }else {
                    iotCubeAc = iotCubeAcs.get(1);
                }
            }
            systemControlDTO.setIotCubeAc(iotCubeAc);
        }
        return systemControlDTO;
    }
}
